<template>
  <el-dialog
    title="编辑委托合同号"
    :visible.sync="visible"
    :before-close="berforClose"
    width="600px"
  >
    <el-form :model="editData" ref="form" :rules="rules" label-width="140px">
      <el-form-item label="借款人/担保客户:">
        <el-input disabled class="form-control-ml" v-model="formData.customer_name"></el-input>
      </el-form-item>
      <el-form-item label="客户类型：">
        <el-input disabled class="form-control-ml" v-model="formData.org_class_name"></el-input>
      </el-form-item>
      <el-form-item label="客户身份证号码/统一社会信用代码:">
        <el-input disabled class="form-control-ml" v-model="formData.lender_idcard"></el-input>
      </el-form-item>
      <el-form-item label="保证合同号：">
        <el-input disabled class="form-control-ml" v-model="formData.gua_contract"></el-input>
      </el-form-item>
      <el-form-item label="委托担保合同号:" prop="ensure_contract">
        <el-input
          placeholder="请输入委托担保合同号"
          class="form-control-ml"
          v-model="editData.ensure_contract"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :loading="saveLoading" class="btn-long" @click="onSubmit">
          确 定
        </el-button>
        <el-button @click="berforClose">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: Boolean,
    formData: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      tableName: 'datum_one_county_industry',
      saveLoading: false,
      editData: {
        ensure_contract: '',
      },
      rules: {
        ensure_contract: [{ required: true, message: '请输入委托担保合同号', trigger: 'blur' }],
      },
    }
  },
  watch: {
    formData: {
      handler: function () {
        const { ensure_contract: ensureContract } = { ...this.formData }
        this.editData.ensure_contract = ensureContract
      },
      immediate: true,
      deep: true,
    },
  },

  created() {},
  methods: {
    berforClose() {
      this.editData = {}
      this.$refs.form.clearValidate()
      this.$emit('update:visible', false)
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.updateData()
        } else {
          this.$message.warning('请输入委托担保合同号')
        }
      })
    },
    async updateData() {
      this.saveLoading = true
      const [res] = await this.$utils.promise(
        this.$api.updateData(
          this.$serviceName,
          this.tableName,
          this.formData.customer_related_id,
          this.editData
        )
      )
      this.saveLoading = false
      if (!res) {
        return this.$message.error(`保存失败，请重试`)
      }

      this.$message.success(`保存成功`)
      this.$emit('success', true)
      this.berforClose()
    },
  },
}
</script>
<style lang="scss" scoped></style>
